package com.alibaba.service;

import com.alibaba.bean.PostLike;
import com.alibaba.bean.PostLikeResult;
import com.alibaba.mapper.PostLikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PostLikeService {

    @Autowired
    private PostLikeMapper postLikeMapper;

    public PostLikeResult like(PostLike postLike) {
        PostLikeResult plr = new PostLikeResult();
        PostLike pl = new PostLike();
        plr.setResult(false);
        try{
            if (postLike.isLike()) {
                postLikeMapper.postLikeInc(postLike.getDailyId());
                postLikeMapper.insertLike(postLike.getUserId(), postLike.getDailyId());
                plr.setErrorMsg("点赞成功");
                plr.setResult(true);
            } else if (postLikeMapper.num(postLike.getDailyId()) != 0) {
                postLikeMapper.postLikeDec(postLike.getDailyId());
                postLikeMapper.deleteLike(postLike.getUserId(), postLike.getDailyId());
                plr.setErrorMsg("点踩成功");
                plr.setResult(true);
            } else {
                plr.setErrorMsg("出错");
            }
        } catch (Exception e) {
            plr.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return plr;
    }

}
